package com.easylinkin.linkappapi.location.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.common.service.CommonService;
import com.easylinkin.linkappapi.location.entity.AppFloorBeacon;
import com.easylinkin.linkappapi.location.mapper.AppFloorBeaconMapper;
import com.easylinkin.linkappapi.location.service.IAppFloorBeaconService;
import com.easylinkin.linkappapi.location.vo.AppFloorBeaconVO;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import com.easylinkin.linkappapi.security.entity.LinkappUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 楼层信标 服务实现类
 *
 * @author JG
 * @since 2023-09-20
 */
@Service
public class AppFloorBeaconServiceImpl extends ServiceImpl<AppFloorBeaconMapper, AppFloorBeacon> implements IAppFloorBeaconService {

    @Resource
    private LinkappUserContextProducer linkappUserContextProducer;

    @Resource
    private CommonService commonService;

    @Resource
    private IAppFloorBeaconService appFloorBeaconService;

    @Autowired
    private AppFloorBeaconMapper appFloorBeaconMapper;

    @Override
    public IPage<AppFloorBeaconVO> getAppFloorList(RequestModel<AppFloorBeaconVO> requestModel) {
        Page page = requestModel.getPage();
        AppFloorBeaconVO appFloorBeaconVO = requestModel.getCustomQueryParams();
        appFloorBeaconVO.setTenantId(linkappUserContextProducer.getNotNullCurrent().getTenantId());
        IPage<AppFloorBeaconVO> result = appFloorBeaconMapper.getBeaconList(page, appFloorBeaconVO);
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateStateBatch(AppFloorBeaconVO appFloorBeaconVO) {
        Assert.notNull(appFloorBeaconVO.getIds(), "id不能为空");
        Assert.notNull(appFloorBeaconVO.getState(), "启用状态不能为空");
        LinkappUser currentUser = linkappUserContextProducer.getNotNullCurrent();
        String[] ids = appFloorBeaconVO.getIds().split(",");
        List<AppFloorBeacon> list = new ArrayList<>();
        for(String id : ids) {
            AppFloorBeacon appFloorBeacon = new AppFloorBeacon();
            appFloorBeacon.setId(Integer.parseInt(id));
            appFloorBeacon.setModifierId(currentUser.getId());
            appFloorBeacon.setTenantId(currentUser.getTenantId());
            commonService.setModifyInfo(appFloorBeacon);
            appFloorBeacon.setState(appFloorBeaconVO.getState());
            list.add(appFloorBeacon);
        }
        return updateBatchById(list);
    }

    @Override
    public RestMessage insertAndUpdate(AppFloorBeacon appFloorBeacon) {
        LinkappUser currentUser = linkappUserContextProducer.getNotNullCurrent();
        appFloorBeacon.setModifierId(currentUser.getId());
        appFloorBeacon.setModifyTime(new Date());
        appFloorBeacon.setTenantId(currentUser.getTenantId());
        if(appFloorBeacon.getId() != null){
            appFloorBeaconService.updateById(appFloorBeacon);
        } else{
            appFloorBeacon.setCreatorId(currentUser.getId());
            appFloorBeacon.setCreateTime(new Date());
            appFloorBeacon.setBindTime(new Date());
            if (checkExist(appFloorBeacon)) {
                return RestBuilders.failureBuilder().message("设备编号不可重复" ).build();
            }
            appFloorBeaconService.save(appFloorBeacon);
        }
        return RestBuilders.successBuilder().message("保存成功" ).build();
    }

    @Override
    public AppFloorBeaconVO queryByBeaconId(Integer id) {
        Assert.notNull(id, "id不能为空");
        LinkappUser currentUser = linkappUserContextProducer.getNotNullCurrent();
        AppFloorBeaconVO vo = new AppFloorBeaconVO();
        vo.setId(id);
        vo.setTenantId(currentUser.getTenantId());
        List<AppFloorBeaconVO> list = appFloorBeaconMapper.getBeaconList(vo);
        if (1 == list.size()) {
            return list.get(0);
        }
        return null;
    }

    private  boolean checkExist(AppFloorBeacon appFloorBeacon) {
        Assert.notNull(appFloorBeacon.getDeviceCode(), "设备编号不能为空");
        QueryWrapper<AppFloorBeacon> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tenant_id_", appFloorBeacon.getTenantId());
        queryWrapper.eq("device_code_", appFloorBeacon.getDeviceCode());
        AppFloorBeacon one = getOne(queryWrapper);
        if (one == null) {
            return false;
        }
        return true;
    }
}
